/**
 * Created by linxiao on 17/8/3.
 */
function login(params) {
    var _this = this
    _this.params = params;
    _this.init();
}
login.prototype.init = function () {
    var _this = this;
    _this.onclick();
    _this.rememberMe();
}
login.prototype.onclick = function () {
    var _this = this;
    var ui = _this.params.ui;
    $(ui).find("[name='submit']").on("click", function () {
        var username = $(ui).find("input[name='username']").val();
        var password = $(ui).find("input[name='password']").val();
        var check_result = _this.checkInfo(username, password);
        if(check_result){
            _this.submit(username, password)
        }
    })
}
//校验
login.prototype.checkInfo = function (jusername, jpassword) {
    if(MY_UI.isEmpty(jusername)){
        Materialize.toast('用户名不能为空!', 3000, '');
        return false;
    }else  if(MY_UI.isEmpty(jpassword)){
        Materialize.toast('密码不能为空!', 3000, '')
        return false;
    }
    return true;
}
//请求:提交
login.prototype.submit = function (jusername, jpassword) {
    var _this = this
    var url = _this.params.ajax_url || "/ajax/login/checklogin";
    console.log(_this.params.ajax_url)
     $.ajax( {
        url:url,
        data:{
            user_name:jusername,
            password:jpassword
        },
        type:'post',
        dataType:'json',
        success:function(data) {
            console.log(data)
            if(data.success == 1){
                console.log("data[0].url==="+data.url);
                var jump_url = data.url || "/index/index/index";
                console.log("jump_url====="+jump_url);
                location.href = jump_url;
            }else if(data.success == 0){
                var message = "";
                switch (data.error.message){
                    case "Not_Found" : message = "用户名或密码错误"; break;
                    case "Invalid" : message = "用户名或密码错误"; break;
                    default :
                        message = "未知错误";
                }
                Materialize.toast(message, 3000, '')
            }
        },
        error : function() {
            Materialize.toast('请求失败,请刷新重试!', 3000, '')
        }
    });
}
//记住用户名密码
login.prototype.rememberMe = function(){
    var _this = this;
    if ($.cookie("remember-me") == "true") {
        $("#remember-me").attr("checked", true);
        $("input[name='username']").val($.cookie("username"));
        $("input[name='password']").val($.cookie("password"));
    }
    $('#remember-me').change(function(){
        var check_status = $(this).is(':checked');
        if (check_status) {
            var str_username = $("input[name='username']").val();
            var str_password = $("input[name='password']").val();
            $.cookie("remember-me", "true", { expires: 7 }); //存储一个带7天期限的cookie
            $.cookie("username", str_username, { expires: 7 });
            $.cookie("password", str_password, { expires: 7 });
        }
        else {
            $.cookie("remember-me", "false", { expire: -1 });
            $.cookie("username", "", { expires: -1 });
            $.cookie("password", "", { expires: -1 });
        }
    });
}
